//
//  OrderDetailViewController.m
//  cm
//
//  Created by suyibaishan on 2017/11/5.
//  Copyright © 2017年 feihu. All rights reserved.
//

#import "OrderDetailViewController.h"
#import "OrderDetailCell.h"

@interface OrderDetailViewController () <UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView;
    NSDictionary *dataSource;
    UIView *headView;
    UILabel *titleLabel;
    UILabel *stateLabel;
    UIView *footView;
    UILabel *totalLabel;
    UILabel *priceLabel;
    UILabel *amountLabel;
    UILabel *remarkLabel;
    UIButton *cancelBtn;
}
@end

@implementation OrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self reloadData];
    
    [self createOrderDetailView];
}

-(void)reloadData
{
    [[ToolNet sharedManager] managerOrders:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] orderId:self.orderId url:MOrderDetail completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            dataSource  = responseJson[@"data"];
            titleLabel.text = [NSString stringWithFormat:@"订单编号：%@",dataSource[@"orderNo"]];
            if ([dataSource[@"state"] isEqual:@0]) {
                stateLabel.text = @"待审核"; 
            }
            else if ([dataSource[@"state"] isEqual:@1]) {
                stateLabel.text = @"订单驳回";
            }
            else if ([dataSource[@"state"] isEqual:@2]) {
                stateLabel.text = @"订单完成";
            }
            else if ([dataSource[@"state"] isEqual:@3]) {
                stateLabel.text = @"订单作废";
                cancelBtn.hidden = YES;
            }
            totalLabel.text = [NSString stringWithFormat:@"订单总金额：¥%.2f",[dataSource[@"orderAmount"] floatValue]];
            priceLabel.text = [NSString stringWithFormat:@"首付金额：¥%.2f",[dataSource[@"downPaymentAmount"] floatValue]];
            amountLabel.text = [NSString stringWithFormat:@"实付金额：¥%.2f",[dataSource[@"payAmount"] floatValue]];
            remarkLabel.text = [NSString stringWithFormat:@"买家留言：%@",dataSource[@"remark"]];
            [_tableView reloadData];
        }
        else {
            [self showMessage:responseJson[@"msg"]];
        }
    }];
}

-(void)createOrderDetailView
{
    self.titleLabel.text = @"订单详情";
    self.backButton.hidden = NO;
    
    [self createHeadView];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.baseBarHeight, kScreenWidth, kScreenHeight - self.baseBarHeight-49) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [self.view addSubview:_tableView];
    _tableView.tableHeaderView = headView;
    
    [self createfootView];
    _tableView.tableFooterView = footView;
    
    cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelBtn.backgroundColor = [UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1];
    [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [cancelBtn setTitle:@"作废订单" forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [cancelBtn addTarget:self action:@selector(cancelBtnHandler) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancelBtn];
    cancelBtn.sd_layout
    .leftSpaceToView(self.view, 0)
    .bottomSpaceToView(self.view, 0)
    .widthIs(kScreenWidth)
    .heightIs(49);
}

-(void)cancelBtnHandler
{
    [[ToolNet sharedManager] managerOrders:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] orderId:[dataSource[@"orderId"] integerValue] url:MCancelOrder completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
//            menberLabel.text = [NSString stringWithFormat:@"• %@    ",responseJson[@"data"][@"realName"]];
//            timeLabel.text = [NSString stringWithFormat:@"加入时间:%@",responseJson[@"data"][@"createTime"]];
//            [_tableView reloadData];
            [self.navigationController popViewControllerAnimated:YES];
            [TSMessage showMessage:@"操作成功" type:TSMessageNotificationTypeSuccess];
        }
        else {
            [self showMessage:responseJson[@"msg"]];
        }

    }];
}

-(void)createHeadView
{
    headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    headView.backgroundColor = [UIColor whiteColor];
    
    UIView *grayline = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    grayline.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [headView addSubview:grayline];
    
    titleLabel = [[UILabel alloc] init];
//    titleLabel.text = [NSString stringWithFormat:@"订单编号：21324354657687"];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.font = [UIFont systemFontOfSize:14];
    [headView addSubview:titleLabel];
    titleLabel.sd_layout
    .leftSpaceToView(headView, 10)
    .topSpaceToView(grayline, 10)
    .widthIs(kScreenWidth - 110)
    .heightIs(20);
    
    stateLabel = [[UILabel alloc] init];
//    stateLabel.text = @"已作废";
    stateLabel.font = [UIFont systemFontOfSize:13];
    stateLabel.textAlignment = NSTextAlignmentRight;
    stateLabel.textColor = [UIColor colorWithRed:0.99 green:0.33 blue:0.16 alpha:1];
    [headView addSubview:stateLabel];
    stateLabel.sd_layout
    .rightSpaceToView(headView, 10)
    .topSpaceToView(grayline, 10)
    .widthIs(100)
    .heightIs(20);
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [headView addSubview:line];
    line.sd_layout
    .leftSpaceToView(headView, 0)
    .bottomSpaceToView(headView, 0)
    .widthIs(kScreenWidth)
    .heightIs(1);
}

-(void)createfootView
{
    footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 120)];
    footView.backgroundColor = [UIColor whiteColor];
    
    totalLabel = [[UILabel alloc] init];
    totalLabel.textAlignment = NSTextAlignmentRight;
//    totalLabel.text = @"订单总金额：¥9111.00";
    totalLabel.textColor = [UIColor blackColor];
    totalLabel.font = [UIFont systemFontOfSize:13];
    [footView addSubview:totalLabel];
    totalLabel.sd_layout
    .rightSpaceToView(footView, 10)
    .topSpaceToView(footView, 5)
    .widthIs(kScreenWidth - 100)
    .heightIs(20);
    
    priceLabel = [[UILabel alloc] init];
    priceLabel.textAlignment = NSTextAlignmentRight;
//    priceLabel.text = [NSString stringWithFormat:@"实付金额：¥9111.00"];
    priceLabel.textColor = [UIColor blackColor];
    priceLabel.font = [UIFont systemFontOfSize:13];
    [footView addSubview:priceLabel];
    priceLabel.sd_layout
    .rightSpaceToView(footView, 10)
    .topSpaceToView(totalLabel, 0)
    .widthIs(kScreenWidth - 100)
    .heightIs(20);
    
    amountLabel = [[UILabel alloc] init];
    amountLabel.textAlignment = NSTextAlignmentRight;
//    priceLabel.text = [NSString stringWithFormat:@"实付金额：¥9111.00"];
    amountLabel.textColor = [UIColor blackColor];
    amountLabel.font = [UIFont systemFontOfSize:13];
    [footView addSubview:amountLabel];
    amountLabel.sd_layout
    .rightSpaceToView(footView, 10)
    .topSpaceToView(priceLabel, 0)
    .widthIs(kScreenWidth - 100)
    .heightIs(20);

    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [footView addSubview:line];
    line.sd_layout
    .leftSpaceToView(footView, 0)
    .topSpaceToView(amountLabel, 5)
    .widthIs(kScreenWidth)
    .heightIs(1);
    
    remarkLabel = [[UILabel alloc] init];
    remarkLabel.numberOfLines = 2;
//    remarkLabel.text = @"买家留言：水电费大喊大叫很高的";
    remarkLabel.textColor = [UIColor blackColor];
    remarkLabel.font = [UIFont systemFontOfSize:13];
    [footView addSubview:remarkLabel];
    remarkLabel.sd_layout
    .leftSpaceToView(footView, 10)
    .topSpaceToView(line, 5)
    .widthIs(kScreenWidth - 20)
    .heightIs(35);
}

#pragma - UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataSource[@"productList"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier =  @"indentifier";
    OrderDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[OrderDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.model = [[OrderDetailModel alloc] initWithDictionary:dataSource[@"productList"][indexPath.row]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
